package com.drakulo.hi.gfx;

import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.UnicodeFont;

/**
 * Représente un bouton
 */
public class Button implements HIGraphicComponent {
	private static final Color DEFAULT_INNER_COLOR = Color.lightGray;
	private static final Color DEFAULT_BORDER_COLOR = Color.black;

	public static Button getOKButton() {
		Button b = new Button("OK");
		b.setWidth(50);

		return b;
	}

	private String text;
	private int x;
	private int y;

	private int width;
	private Color borderColor;

	private Color innerColor;

	private boolean okClicked;

	public Button(String text) {
		this.text = text;
		this.borderColor = DEFAULT_INNER_COLOR;
		this.innerColor = DEFAULT_BORDER_COLOR;
	}

	public int getWidth() {
		return this.width;
	}

	public int getX() {
		return this.x;
	}

	public int getY() {
		return this.y;
	}

	public boolean isOkClicked() {
		return this.okClicked;
	}

	@Override
	public boolean mouseIsOnIt(int mouseX, int mouseY) {
		if (mouseX < this.x || mouseY < this.y || mouseX > this.x + this.width
				|| mouseY > this.y + 20) {
			return false;
		}
		return true;
	}

	@Override
	public void render(Graphics g) throws SlickException {
		render(g, this.x, this.y);
	}

	@Override
	public void render(Graphics g, int x, int y) throws SlickException {
		UnicodeFont font;
		font = FontFactory.getDefaultFont();

		g.setColor(this.innerColor);
		g.fillRect(x, y, this.width, 20);
		g.setColor(this.borderColor);
		g.drawRect(x, y, this.width, 20);

		font.drawString(x + 15, y + 2, this.text);
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public void setX(int x) {
		this.x = x;
	}

	public void setY(int y) {
		this.y = y;
	}

	@Override
	public void update(Input input) {

		if (mouseIsOnIt(input.getMouseX(), input.getMouseY())) {
			// innerColor = Color.blue;
			// borderColor = DEFAULT_BORDER_COLOR;
			if (input.isMousePressed(Input.MOUSE_LEFT_BUTTON)) {
				this.okClicked = true;
			}
		} else {
			this.innerColor = DEFAULT_INNER_COLOR;
			this.borderColor = DEFAULT_BORDER_COLOR;
		}
	}
}
